package com.sphd.emanu.activity.addgoodfrom;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sphd.emanu.R;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.pojo.CategoryAndStorageEntity;
import com.sphd.emanu.pojo.goods.Category;
import com.sphd.emanu.pojo.goods.Commodity;
import com.sphd.emanu.pojo.goods.Composition;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.pojo.material.Material;
import com.sphd.emanu.pojo.material.Storage;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 新增物料 和编辑物料共用的界面
 *
 * @Author Hu
 * @Time 2017/5/9 -- 17:02.
 * @Email 87683202@qq.com
 */
public class AddGoodFromActivity extends MVPBaseActivity<AddGoodFromContract.View, AddGoodFromPresenter> implements AddGoodFromContract.View {

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    Intent intent;
    Bundle bundle;
    Composition composition;
    int flag;

    @BindView(R.id.btn_save)
    TextView btnSave;
    @BindView(R.id.item_category)
    TextView itemCategory;
    @BindView(R.id.item_edit_amount)
    EditText itemEditAmount;
    @BindView(R.id.item_amount)
    TextView itemAmount;
    @BindView(R.id.item_edit)
    TextView itemEdit;
    @BindView(R.id.item_del)
    TextView itemDel;
    @BindView(R.id.item_edit_del)
    LinearLayout itemEditDel;
    @BindView(R.id.item_name)
    TextView itemName;
    @BindView(R.id.item_specifications)
    TextView itemSpecifications;
    User user;
    CategoryAndStorageEntity categoryAndStorageEntity;
    Commodity commodity;
    int cateId = 0;
    int mId = 0;
    String cateName = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_good_from);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
    }

    private void initView() {
        itemEditDel.setVisibility(View.GONE);
        intent = getIntent();
        bundle = intent.getExtras();
        commodity = (Commodity) bundle.getSerializable(Constant.COMMODITY);
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        flag = bundle.getInt(Constant.FLAG);
        if (flag == 1) {
            setTitleBar(toolbar, "编辑原辅材料");
            composition = (Composition) bundle.getSerializable(Constant.COMPOSOTION);
            itemName.setText(composition.getName());
            itemSpecifications.setText(composition.getSpecifications());
            itemCategory.setText(composition.getModel());
            itemEditAmount.setText(String.valueOf(composition.getAmount()));
        } else {
            setTitleBar(toolbar, "新增原辅材料");
        }
    }

    private void getList() {
        mPresenter.categorAndStorage(user.getOid(), commodity.getCategory());
    }


    public void onViewClicked() {

    }

    @Override
    public void setRes(int res) {
        dissProgressDialog();
        if (res == 1) {
            toast("操作成功");
            setResult(Constant.REQUEST_CODE);
            finish();
        } else {
            toast("操作失败，请重试。");
        }
    }

    @Override
    public void setEntity(CategoryAndStorageEntity categoryAndStorageEntity) {
        this.categoryAndStorageEntity = categoryAndStorageEntity;
        if (categoryAndStorageEntity != null)
            showList(categoryAndStorageEntity.getCategory(), 0);
    }

    private void showList(final List<?> list, final int type) {
        final String[] strings = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            strings[i] = (type == 0 ? (((Category) list.get(i)).getParentName() + "-" + ((Category) list.get(i)).getName()) :
                    (((Material) list.get(i)).getName()));
        }

        Dialog alertDialog = new AlertDialog.Builder(this).
                setTitle(type == 0 ? "请选择分类？" : "请选择物料名称？").
                setIcon(R.drawable.ic_launcher)
                .setItems(strings, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (type == 0) {
                            cateId = ((Category) list.get(which)).getId();
                            cateName = ((Category) list.get(which)).getName();
                            mPresenter.selectNextCategory(((Category) list.get(which)).getId());
                        } else {
                            mId=((Material) list.get(which)).getId();
                            itemName.setText(((Material) list.get(which)).getName());
                            itemSpecifications.setText(((Material) list.get(which)).getSpecifications());
                            itemCategory.setText(cateName);
                        }
                    }
                }).setNegativeButton("取消", null).
                        create();
        alertDialog.show();
    }

    @Override
    public void setCate(Category category) {
        if (category != null)
            if (category.getListMaterical().size() != 0){

                showList(category.getListMaterical(), 1);
            }else {
                toast("该分类暂无物料可选，请编辑后选择。");
            }
    }

    @OnClick({R.id.btn_save, R.id.item_name})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_save:
                if (!TextUtils.isEmpty(itemEditAmount.getText().toString().trim())) {
                    showProgressDialog();
                    if (flag == 0) {
                        mPresenter.addComposition(commodity.getId(),itemName.getText().toString().trim(),
                                itemSpecifications.getText().toString().trim(),
                                itemCategory.getText().toString().trim()
                                , Integer.valueOf(itemEditAmount.getText().toString().trim()),mId);
                    } else {
                mPresenter.updateComposition(composition.getId(), itemName.getText().toString().trim(),
                        itemSpecifications.getText().toString().trim(), itemCategory.getText().toString().trim()
                        , Integer.valueOf(itemEditAmount.getText().toString().trim()));

                    }

                } else {
                    toast("请按提示填写。");
                }



                break;
            case R.id.item_name:
                getList();
                break;
        }
    }
}
